package cn.edu.nju.freshdelivery.action.buyer;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.nju.freshdelivery.constant.Result;
import cn.edu.nju.freshdelivery.model.Buyer;
import cn.edu.nju.freshdelivery.service.impl.BuyerServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

@Component
public class BuyerRegisterAction extends ActionSupport {
	private Buyer buyer;
	private BuyerServiceImpl buyerService;
	private String result;

	public Buyer getBuyer() {
		return buyer;
	}

	public void setBuyer(Buyer buyer) {
		this.buyer = buyer;
	}

	/*
	 * public BuyerServiceImpl getBuyerService() { return buyerService; }
	 */
	@Resource
	public void setBuyerService(BuyerServiceImpl buyerService) {
		this.buyerService = buyerService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	@Override
	public String execute() throws Exception {
		/**
		 * TODO:服务器端验证合法性
		 */
		result = SUCCESS;
		if (buyerService.findByUsername(buyer.getUsername()) != null)
			result = Result.EXISTED;
		buyerService.add(buyer);
		return SUCCESS;
	}
}
